<script setup>
import { onMounted, ref } from 'vue';
import {deleteCompany, getAllCompany} from '../api/company.js'
import router from '@/router/index.js';

// let pageNumber = 2 //指定页码
let companyList = ref([])
let pages=ref() //总页数
let current=ref() //当前页数
let totals = ref() //总记录数

    onMounted(()=>{
        getAll()
    })
//查看
    async function getAll(pageNumber){
        const  response = await getAllCompany(pageNumber)
        console.log(response)
        console.log("---------------")
        console.log(" response.data.data.current:"+response.data.data.current)
        current.value = response.data.data.current
        console.log("---------------")
        console.log(" response.data.data.pages:" , response.data.data.pages)
        pages.value = response.data.data.pages
        console.log("---------------")
        console.log(" response.data.data.total:" , response.data.data.total)
        totals.value = response.data.data.total
        console.log("---------------")
        console.log( response.data.data.records )
        companyList.value = response.data.data.records
    }
//-----------------------------------------------------------------
//删除
    async function delCompany(id) {
        const response =  await deleteCompany(id)
        console.log("---------删除的response-------------")
        console.log(response)
        //刷新数组
         companyList.value = companyList.value.filter(item => item.companyId !== id)
    }
//编辑-----------------------------------------------------------------
function EditCompany(companyId){
    console.log(companyId)
    router.push({path:`companyEdit/${companyId}`})
}


</script>

<template>
    <div>
        <h2>分页查询</h2>
        <ul>
            <li v-for="company in companyList " :key="company.companyId">
                {{ company.companyId }},
                <RouterLink :to="`/companyDetail/${company.companyId}`"> {{ company.companyName }},</RouterLink>
                {{ company.companyEmail }},
                {{ company.companyPhone }},
                {{ company.companyCity }},
                {{ company.companyIndustry }},
                {{ company.companySize }},
                <!-- <RouterLink :to="`/companyEdit/${company.companyId}`">编辑</RouterLink> -->
                <button @click="EditCompany( company.companyId )">编辑</button>
                <button @click="delCompany( company.companyId)">删除</button>
            </li>
        </ul>
        <button v-if="current!=1" @click="getAll(current-1)">上一页</button>
        当前页面是第{{ current }} 页，一共{{ pages }} 页
        <button v-if="current!=pages" @click="getAll(current+1)">下一页</button>
        
    </div>
</template>